package com.saicmotor.hk2.controller;

import com.saicmotor.hk2.model.UserRole;
import com.saicmotor.hk2.model.UserRoleInfo;
import com.saicmotor.hk2.service.CommonService;
import com.saicmotor.hk2.service.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user_role")
public class UserRoleController {
    @Autowired
    UserRoleService userRoleService;
    @Autowired
    CommonService commonService;

    @RequestMapping("/add")
    public String insertUserRoleInfo(String userName, String roleName) {
        if (userRoleService.checkRecord(userName, roleName)) {
            return "This Record is EXISTS";
        } else if (!commonService.checkName("user", userName)) {
            return "userName is NOT EXIST";
        } else if (!commonService.checkName("role", roleName)) {
            return "roleName is NOT EXIST";
        } else {
            userRoleService.insertUserRoleInfo(userName, roleName);
            return "Insert [" + userName + "] plays [" + roleName + "] Complete";
        }
    }

    @RequestMapping("/all")
    public List<UserRole> getAll() {
        return userRoleService.getAll();
    }

    @RequestMapping("/info")
    public List<UserRoleInfo> getAllInfo() {
        return userRoleService.getAllInfo();
    }

    @RequestMapping(value = "/delete", params = "userName")
    public String deleteByUserName(String userName) {
        userRoleService.deleteByUserName(userName);
        return "Delete User [" + userName + "] Complete";
    }

    @RequestMapping(value = "/delete", params = "roleName")
    public String deleteByRoleName(String roleName) {
        userRoleService.deleteByRoleName(roleName);
        return "Delete Role [" + roleName + "] Complete";
    }

    @RequestMapping(value = "/update", params = {"oldUserName", "newUserName", "roleName"})
    public String updateUser(String oldUserName, String newUserName, String roleName) {
        if(!userRoleService.checkRecord(oldUserName, roleName)){
            return "Cannot Found Old Record [" + oldUserName + "] plays [" + roleName + "]";
        } else if (userRoleService.checkRecord(newUserName, roleName)) {
            return "This Record [" + newUserName + "] plays [" + roleName + "] Record is EXISTS";
        } else if (!commonService.checkName("user", newUserName)||
                !commonService.checkName("user", oldUserName)||
                !commonService.checkName("role", roleName)) {
            return "Used Info is NOT EXIST";
        } else {
            userRoleService.updateUser(oldUserName, newUserName, roleName);
            return "Update [" + newUserName + "] plays [" + roleName + "] Complete";
        }
    }

    @RequestMapping(value = "/update", params = {"oldRoleName", "newRoleName", "userName"})
    public String updateRole(String oldRoleName, String newRoleName, String userName) {
        if(!userRoleService.checkRecord(userName, oldRoleName)){
            return "Cannot Found Old Record [" + userName + "] plays [" + oldRoleName + "]";
        } else if (userRoleService.checkRecord(userName, newRoleName)) {
            return "This Record [" + userName + "] plays [" + newRoleName + "] Record is EXISTS";
        } else if (!commonService.checkName("user", userName)||
                !commonService.checkName("role", oldRoleName)||
                !commonService.checkName("role", newRoleName)) {
            return "Used Info is NOT EXIST";
        } else {
            userRoleService.updateRole(oldRoleName, newRoleName, userName);
            return "Update [" + userName + "] plays [" + newRoleName + "] Complete";
        }
    }

    @RequestMapping("/create")
    public  String createUserRoleTable(){
        if (commonService.checkTable("user_role")) {
            return "\"user_role\" Table Exists";
        } else {
            userRoleService.createUserRoleTable("user_role");
            return "Create \"user_role\" Table Complete";
        }
    }
}
